package main

import (
	"aqchain/pkg/entity"
	"aqchain/pkg/utils"
	"fmt"
	"github.com/swaggest/openapi-go/openapi3"
	"log"
	"net/http"
	"os"
	"path"
)

func main() {
	reflector := openapi3.Reflector{}
	reflector.Spec = &openapi3.Spec{Openapi: "3.0.3"}
	reflector.Spec.Info.
		WithTitle("安权链 API").
		WithVersion("0.0.1").
		WithDescription("安权链 API 文档 使用openapi3")

	serverLocal := openapi3.Server{}
	serverLocal.WithURL("http://127.0.0.1:8080")
	serverLocal.WithDescription("本地测试环境")

	serverCloud := openapi3.Server{}
	serverCloud.WithURL("http://aqchain_web.xyz:8080")
	serverCloud.WithDescription("腾讯云测试环境")

	reflector.Spec.WithServers(serverLocal, serverCloud)

	upload := openapi3.Operation{}
	upload.WithTags("item")
	upload.WithSummary("上传作品资源")
	upload.WithDescription("上传作品资源")
	upload.WithID("upload-item")
	utils.Fatal(reflector.SetRequest(&upload, new(entity.UploadItemForm), http.MethodPost))
	utils.Fatal(reflector.SetJSONResponse(&upload, new(entity.ResponseItemData), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&upload, new(entity.ResponseItemData), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&upload, new(entity.ResponseItemData), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodPost, "/api/upload", upload))

	getCertificateOp := openapi3.Operation{}
	getCertificateOp.WithTags("item")
	getCertificateOp.WithSummary("证书获取")
	getCertificateOp.WithDescription("证书获取")
	getCertificateOp.WithID("get-certificate")
	utils.Fatal(reflector.SetRequest(&getCertificateOp, new(entity.GetCertificateUri), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getCertificateOp, new(string), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getCertificateOp, new(entity.Response), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getCertificateOp, new(entity.Response), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/certificate/{dci}", getCertificateOp))

	postAccountOp := openapi3.Operation{}
	postAccountOp.WithTags("account")
	postAccountOp.WithSummary("钱包账户信息创建")
	postAccountOp.WithDescription("钱包账户信息创建")
	postAccountOp.WithID("post-account")
	utils.Fatal(reflector.SetRequest(&postAccountOp, new(entity.PostAccountForm), http.MethodPost))
	utils.Fatal(reflector.SetJSONResponse(&postAccountOp, new(entity.PostAccountForm), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&postAccountOp, new(entity.PostAccountForm), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&postAccountOp, new(entity.PostAccountForm), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodPost, "/api/account", postAccountOp))

	getAccountOp := openapi3.Operation{}
	getAccountOp.WithTags("account")
	getAccountOp.WithSummary("钱包账户信息获取")
	getAccountOp.WithDescription("钱包账户信息获取")
	getAccountOp.WithID("get-account")
	utils.Fatal(reflector.SetRequest(&getAccountOp, new(entity.GetAccountUri), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getAccountOp, new(entity.GetAccountResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getAccountOp, new(entity.GetAccountResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getAccountOp, new(entity.GetAccountResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/account/{address}", getAccountOp))

	putAccountOp := openapi3.Operation{}
	putAccountOp.WithTags("account")
	putAccountOp.WithSummary("钱包账户信息修改")
	putAccountOp.WithDescription("钱包账户信息修改")
	putAccountOp.WithID("put-account")
	utils.Fatal(reflector.SetRequest(&putAccountOp, new(entity.PostAccountForm), http.MethodPut))
	utils.Fatal(reflector.SetJSONResponse(&putAccountOp, new(entity.GetAccountResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&putAccountOp, new(entity.GetAccountResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&putAccountOp, new(entity.GetAccountResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodPut, "/api/account", putAccountOp))

	getItemOp := openapi3.Operation{}
	getItemOp.WithTags("item")
	getItemOp.WithSummary("获取作品记录")
	getItemOp.WithDescription("获取作品记录")
	getItemOp.WithID("get-item")
	utils.Fatal(reflector.SetRequest(&getItemOp, new(entity.GetItemUri), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getItemOp, new(entity.GetItemResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getItemOp, new(entity.GetItemResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getItemOp, new(entity.GetItemResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/item/{dci}", getItemOp))

	getItemsOp := openapi3.Operation{}
	getItemsOp.WithTags("item")
	getItemsOp.WithSummary("获取作品记录集合")
	getItemsOp.WithDescription("获取作品记录集合")
	getItemsOp.WithID("get-items")
	utils.Fatal(reflector.SetRequest(&getItemsOp, new(entity.GetItemsQuery), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getItemsOp, new(entity.GetItemsResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getItemsOp, new(entity.GetItemsResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getItemsOp, new(entity.GetItemsResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/items/", getItemsOp))

	getItemEventsOp := openapi3.Operation{}
	getItemEventsOp.WithTags("item")
	getItemEventsOp.WithSummary("获取作品合约事件记录集合")
	getItemEventsOp.WithDescription("获取作品合约事件记录集合")
	getItemEventsOp.WithID("get-itemEvents")
	utils.Fatal(reflector.SetRequest(&getItemEventsOp, new(entity.GetItemEventsQuery), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getItemEventsOp, new(entity.GetItemEventsResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getItemEventsOp, new(entity.GetItemEventsResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getItemEventsOp, new(entity.GetItemEventsResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/itemEvents", getItemEventsOp))

	getSaleOp := openapi3.Operation{}
	getSaleOp.WithTags("sale")
	getSaleOp.WithSummary("获取交易状态")
	getSaleOp.WithDescription("获取交易状态")
	getSaleOp.WithID("get-sale")
	utils.Fatal(reflector.SetRequest(&getSaleOp, new(entity.GetSaleUri), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getSaleOp, new(entity.GetSaleResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getSaleOp, new(entity.GetSaleResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getSaleOp, new(entity.GetSaleResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/sale/{dci}", getSaleOp))

	getSalesOp := openapi3.Operation{}
	getSalesOp.WithTags("sale")
	getSalesOp.WithSummary("获取交易状态集合")
	getSalesOp.WithDescription("获取交易状态集合")
	getSalesOp.WithID("get-sales")
	utils.Fatal(reflector.SetRequest(&getSalesOp, new(entity.GetSalesQuery), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getSalesOp, new(entity.GetSalesResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getSalesOp, new(entity.GetSalesResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getSalesOp, new(entity.GetSalesResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/sales", getSalesOp))

	getAuction := openapi3.Operation{}
	getAuction.WithTags("auction")
	getAuction.WithSummary("获取拍卖交易状态")
	getAuction.WithDescription("获取拍卖交易状态")
	getAuction.WithID("get-auction")
	utils.Fatal(reflector.SetRequest(&getAuction, new(entity.GetAuctionUri), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getAuction, new(entity.GetAuctionResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getAuction, new(entity.GetAuctionResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getAuction, new(entity.GetAuctionResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/auction/{dci}", getAuction))

	getAuctions := openapi3.Operation{}
	getAuctions.WithTags("auction")
	getAuctions.WithSummary("获取拍卖交易状态集合")
	getAuctions.WithDescription("获取拍卖交易状态集合")
	getAuctions.WithID("get-auctions")
	utils.Fatal(reflector.SetRequest(&getAuctions, new(entity.GetAuctionsQuery), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getAuctions, new(entity.GetAuctionsResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getAuctions, new(entity.GetAuctionsResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getAuctions, new(entity.GetAuctionsResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/auctions", getAuctions))

	getAuctionEvents := openapi3.Operation{}
	getAuctionEvents.WithTags("auction")
	getAuctionEvents.WithSummary("获取拍卖交易状态")
	getAuctionEvents.WithDescription("获取拍卖交易状态")
	getAuctionEvents.WithID("get-auctionEvents")
	utils.Fatal(reflector.SetRequest(&getAuctionEvents, new(entity.GetAuctionEventsQuery), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getAuctionEvents, new(entity.GetAuctionEventsResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getAuctionEvents, new(entity.GetAuctionEventsResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getAuctionEvents, new(entity.GetAuctionEventsResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/auctionEvents", getAuctionEvents))

	getLicense := openapi3.Operation{}
	getLicense.WithTags("license")
	getLicense.WithSummary("获取授权交易状态")
	getLicense.WithDescription("获取授权交易状态")
	getLicense.WithID("get-license")
	utils.Fatal(reflector.SetRequest(&getLicense, new(entity.GetLicenseUri), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getLicense, new(entity.GetLicenseResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getLicense, new(entity.GetLicenseResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getLicense, new(entity.GetLicenseResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/license/{dci}", getLicense))

	getLicenses := openapi3.Operation{}
	getLicenses.WithTags("license")
	getLicenses.WithSummary("获取授权交易状态集合")
	getLicenses.WithDescription("获取授权交易状态集合")
	getLicenses.WithID("get-licenses")
	utils.Fatal(reflector.SetRequest(&getLicenses, new(entity.GetLicensesQuery), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getLicenses, new(entity.GetLicensesResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getLicenses, new(entity.GetLicensesResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getLicenses, new(entity.GetLicensesResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/licenses", getLicenses))

	getLicenseRecords := openapi3.Operation{}
	getLicenseRecords.WithTags("license")
	getLicenseRecords.WithSummary("获取授权交易状态集合")
	getLicenseRecords.WithDescription("获取授权交易状态集合")
	getLicenseRecords.WithID("get-licenseRecords")
	utils.Fatal(reflector.SetRequest(&getLicenseRecords, new(entity.GetLicenseRecordsQuery), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getLicenseRecords, new(entity.GetLicenseRecordsResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getLicenseRecords, new(entity.GetLicenseRecordsResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getLicenseRecords, new(entity.GetLicenseRecordsResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/licenseRecords", getLicenseRecords))

	getTrades := openapi3.Operation{}
	getTrades.WithTags("trade")
	getTrades.WithSummary("获取所有交易状态集合")
	getTrades.WithDescription("获取所有交易状态集合")
	getTrades.WithID("get-trades")
	utils.Fatal(reflector.SetRequest(&getTrades, new(entity.GetTradesQuery), http.MethodGet))
	utils.Fatal(reflector.SetJSONResponse(&getTrades, new(entity.GetTradesResponse), http.StatusOK))
	utils.Fatal(reflector.SetJSONResponse(&getTrades, new(entity.GetTradesResponse), http.StatusBadRequest))
	utils.Fatal(reflector.SetJSONResponse(&getTrades, new(entity.GetTradesResponse), http.StatusInternalServerError))
	utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/trades", getTrades))

	//getBoughtLicense := openapi3.Operation{}
	//getBoughtLicense.WithTags("license")
	//getBoughtLicense.WithSummary("获取授权记录")
	//getBoughtLicense.WithDescription("获取授权记录")
	//getBoughtLicense.WithID("get-licenseRecord")
	//utils.Fatal(reflector.SetRequest(&getBoughtLicense, new(entity.GetLicenseRecordsQuery), http.MethodGet))
	//utils.Fatal(reflector.SetJSONResponse(&getBoughtLicense, new(entity.GetLicenseRecordResponse), http.StatusOK))
	//utils.Fatal(reflector.SetJSONResponse(&getBoughtLicense, new(entity.GetLicenseRecordResponse), http.StatusBadRequest))
	//utils.Fatal(reflector.SetJSONResponse(&getBoughtLicense, new(entity.GetLicenseRecordResponse), http.StatusInternalServerError))
	//utils.Fatal(reflector.Spec.AddOperation(http.MethodGet, "/api/license", getBoughtLicense))

	schema, err := reflector.Spec.MarshalJSON()
	if err != nil {
		log.Fatal(err)
	}

	wd, err := os.Getwd()
	if err != nil {
		return
	}
	utils.Fatal(os.WriteFile(path.Join(wd, "cmd", "openapi", "openapi.json"), schema, 0777))
	fmt.Println(string(schema))
}
